<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-02-22
 * Time: 16:49:00
 * Info: 后台配置控制器
 */

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\Config as ConfigModel;
use think\facade\Db;
use think\facade\View;
use libs\Form;

class Config extends Base
{

    /**
     * 后台配置列表
     */
    public function index()
    {
        $groupName = ConfigModel::GROUP_LIST;

        //获取表前缀
        $table_prefix = config("database.connections.mysql.prefix");

        //字段值group 使用any_value方法
        $sql   = "select any_value(`id`) as id,any_value(`group`) as `new_group` ,any_value(`weigh`) as weigh  from `".$table_prefix."config` where  `group` <> '99' GROUP BY `new_group`  ORDER BY `weigh` ASC,`id` ASC";
        $group = Db::query($sql);

        $group2 = [];
        foreach ($group as $key => $v) {
            $v1['group_en']   = $v['new_group'];
            $v1['group_name'] = ! empty($groupName[$v['new_group']]) ? $groupName[$v['new_group']] : '';
            $group2[]         = $v1;
        }

        $newList = [];
        $list    = ConfigModel::getConfigList();
        foreach ($list as $k => $v) {
            if (isset($v['fieldtype']) && $v['fieldtype'] == 'ueditor_icon') {
                $v['value'] = array_filter(explode(',', $v['value']));
            }
            if (isset($v['fieldtype']) && ($v['fieldtype'] == 'radio' || $v['fieldtype'] == 'select')) {
                $vSetting   = ! empty($v['setting']) ? json_decode($v['setting'], true) : '';
                $newSetting = [];
                foreach ($vSetting as $s) {
                    if (strpos($s, ':') !== false) {
                        $sArr                 = explode(":", $s);
                        $newSetting[$sArr[1]] = $sArr[0];
                        $v['setting']         = ($newSetting);
                    } else {
                        $v['setting'] = $vSetting;
                    }
                }
            }
            $newList[$v['group']][] = $v;
        }

        $ueditorIcon = config('ueditor_icon');
        View::assign('lists', $newList);
        View::assign('group', $group2);
        View::assign('ueditor_icon', $ueditorIcon);

        return View::fetch();
    }

    /**
     * 保存设置
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param('row');
            $res    = ConfigModel::saveData($params);

            $this->success($res['msg'], '', '', $res['url']);
        }
    }

    /**
     * 自定义配置
     */
    public function customConfig()
    {
        $data = ConfigModel::where('group', 99)->select();
        View::assign('data', $data);

        return View::fetch();
    }

    /**
     * 添加自定义配置
     */
    public function customConfigAdd()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $res = ConfigModel::customAdd($param);

            return json($res);

        }

        return View::fetch();
    }

    /**
     * 修改自定义配置
     */
    public function customConfigEdit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $res = ConfigModel::customEdit($param);

            return json($res);

        }

        $id   = $this->request->param('id');
        $data = ConfigModel::where(['id' => $id])->find();
        View::assign('data', $data);

        return View::fetch();
    }

    /**
     * 删除自定义配置
     */
    public function customConfigDel()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            ConfigModel::customDel($id);
            $this->success('删除成功');
        }
    }

}
